# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: 无序数组子数组和为指定值的最长长度.py
@time: 2021/02/01
"""


def find_max_sub(nums: list, k):
    ...
    map = {}

    map[0] = -1
    max_len = 0
    sum = 0
    for i in range(len(nums)):
        ...
        sum = sum + nums[i]
        if (sum - k) in map:
            ...
            max_len = max(max_len, i - map[sum - k])
        if sum not in map:
            map[sum] = i

    return max_len


if __name__ == '__main__':
    pass
    result = find_max_sub([1, 1, 1, 2, 2], 3)
    print(result)
